/*
 * name:数字证书核心组件 CertCore
 * description:不包括form控件设置
 * author:yanmu.wj
 * date:2011-11-23
 *
 * @exports
 *      support:Boolean
 *      result:{
 *          serialNumber:String,
 *          signResult:
 *      }
 */
KISSY.add('CertCore',function(S){
    var CertCore = function(cfg){
        this.objectId = cfg.id || '';
        this.certType = cfg.certType || '';
        this.serialNumber = cfg.serialNumber || '';
        this.oldSerialNumber = cfg.oldSerialNumber || '';
        this.signParam = cfg.signParam || '';
        this.callback = S.isFunction(cfg.callback) && cfg.callback || function(){};
        
        //iTtrusPTA
        this.iTrusPTA = null;

        this.support = true;
        this.result = {
            serialNumber:'',
            signResult:'',
            oldIssuer:'',
            oldSerialNumber:'',
            oldSignResult:''
        }
    };
    var _CertCore = function(cfg){
        return new Cert(cfg);
    };
    S.augment(CertCore,{
        OUTPUT_BASE64:0x04,
        errorCode:{
            Not4OS:1, //系统不支持
            Not4UA:2, //浏览器不支持
            NoPTAInIE:3, //IE中获取证书失败
            NoPTAInNotIE:4, //非IE中获取证书失败
            NotCertUser:5, //非数字证书用户
            NoMachineCert:6，
            NoUKEYCert:7,
            NoICBCCert:8
        },
        init:function(){
            //证书不支持非windows系统
            if(navigator.appVersion.indexOf("Win") < 0){
                this.support = false;
                this.callback(this.errorCode.Not4OS)
                return this;
            //不支持非IE,Firefox,Chrome浏览器
            }else if(!S.UA.ie || !S.UA.firfox || !S.UA.chrome){
                this.support = false;
                this.callback(this.errorCode.Not4UA)
                return this;
            }else{
                //获取iTrusPTA
                this.support = !!this.iTrusPTA;
                this.iTrusPTA = this.getPTA();
                return this;
            }

            this.check();

            return this;
        },
        check:function(){
            if(!this.iTrusPTA || !this.iTrusPTA.Filter) return;
            
            var certFilter = this.iTrusPTA.Filter, //生成过滤器
                certs,
                cert = null,
                errorCode;

            //清除过滤器
            certFilter.clear();

            switch(this.certType.toUpperCase()){
                case 'MACHINE':
                    errorCode = this.errorCode.NoMachineCert;
                    certFilter.SerialNumber = this.getSerialNumber();
                    break;
                case 'UKEY':
                    errorCode = this.errorCode.NoUKEYCert;
                    certFilter.SerialNumber = this.getSerialNumber();
                    break;
                case 'ICBC':
                    errorCode = this.errorCode.NoICBCCert;
                    certFilter.SerialNumber = this.getSerialNumber();
                    break;
                default:
                    this.callback(this.errorCode.NotCertUser);
                    break;
            }

            //证书列表
            certs = this.iTrusPTA.MyCertificates,

            for(var i=certs.Count;i-->0;){
                if(this.serialNumber.indexOf(certs(i).SerialNumber) >= 0){
                    cert = certs(i); //certs is not Array
                    break;
                }
            }

            //未找到证书
            if(!cert){
                this.callback(errorCode);
                return;
            }

            //数字证书签名
            try{
                //机器证书签名异常为个案，暂不对异常做处理
                this.result.signResult = cert.SignLogonData(this.signParam,this.OUTPUT_BASE64);
                this.result.serialNumber = cert.SerialNumber;
            }catch(e){}


            this.checkOldCert();
        },
        //校验文件证书
        checkOldCert:function(){
            if(!this.certType.toUpperCase() == 'MACHINE' || !this.oldSerialNumber) return;

            var certFilter = this.iTrusPTA.Filter, //生成过滤器
                oldCerts,
                oldCert = null;

            //清除过滤参数
            certFilter.Clear();
            //设置过滤器
            certFilter.SerialNumber = this.getSerialNumber(this.oldSerialNumber);
            //老证书列表
            oldCerts = iTrusPTA.MyCertificates;

            for(var i=oldCerts.Count;i-->0;){
                if(this.oldSerialNumber.indexOf(oldCerts(i).SerialNumber) >= 0){
                    oldCert = oldCerts(i); //certs is not Array
                    break;
                }
            }

            if(!oldCert) return;

            //文件证书存在
            if(oldCert){
                try{
                    //校验结果
                    this.result.oldSignResult = oldCert.SignLogonData(cfg.signParam,OUTPUT_BASE64);
                    this.result.oldIssuer = oldCert.Issuer;
                    this.result.oldSerialNumber = oldCert.SerialNumber;
                }catch(e){}
            }
        },
        getSerialNumber:function(type){
            var sn = type && type.toUpperCase()=='OLD' ? this.oldSerialNumber : this.serialNumber;
            return (parseInt(sn(0,1),16) > 8) ? '00' + sn : sn;
        },
        getPTA:function(){
            if(Window.ActiveXObject){
                this.getPTA = this.getPTAInIE;
                return this.getCertInIE();
            }else{
                this.getPTA = this.getPTAInNotIE;
                return this.getCertInNotIE();
            }
        },
        createObject:function(id){
            var obj = S.DOM.create('<object>',{
                id:id
            });
            return obj;
        },
        //IE中获取iTrusPTA
        getPTAtInIE:function(){
            var iTrusPTA = null;
            if(Window.ActiveXObject){
                try{
                    iTrusPTA = new Window.ActiveXObject('PTA.iTrusPTA');
                    if(!iTursPTA && !iTrusPTA.Filter){
                        this.callback(this.errorCode.NoPTAInIE);
                        return null;
                    }
                }catch(e){
                    this.callback(this.errorCode.NoPTAInIE);
                }
            }
            return iTrusPTA;
        },
        //非IE中获取iTrusPTA
        getPTAInNotIE:function(){
            var iTrusPTA = null;
            if(!cfg.objectId){
                this.callback(this.errorCode.NoPTAInNotIE);
                return null;
            }else{
                iTrusPTA = S.one(cfg.objectId) || this.createObject(cfg.objectId);
                if(!iTursPTA && !iTrusPTA.Filter){
                    this.callback(this.errorCode.NoPTAInNotIE);
                    return null;
                }
            }
            return iTrusPTA;
        }
    });

    return _CertCore;
});
